package com.doyens.item.controller;

import com.doyens.item.pojo.SpecGroup;
import com.doyens.item.pojo.SpecParam;
import com.doyens.item.service.SpecificationService;
import com.sun.corba.se.impl.orbutil.graph.Graph;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.security.acl.Group;
import java.util.Collection;
import java.util.List;

@Controller
@RequestMapping("spec")
public class SpecificationController {


    @Autowired
    private SpecificationService specificationService;

    /**
     * 查询所有规格组信息
     * @param cid
     * @return
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupByCid(@PathVariable("cid") Long cid){
        List<SpecGroup> specGroups = specificationService.queryGroupByCid(cid);
        if (CollectionUtils.isEmpty(specGroups)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specGroups);
    }

    /**
     *
     * @param gid  根据规格组id查询规格参数信息
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
                    @RequestParam(value = "generic",required = false) Boolean generic,
                    @RequestParam(value = "searching",required = false) Boolean searching
    ){
        //调用service
        List<SpecParam>  paramList = this.specificationService.queryParams(gid,cid,generic,searching);
        if (CollectionUtils.isEmpty(paramList)){

            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(paramList);
    }

    //根据分类id查询规格组
    @GetMapping("group/param/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupWithParam(@PathVariable("cid") Long cid){
        List<SpecGroup> specGroups = this.specificationService.queryGroupWithParamByCid(cid);
        if (CollectionUtils.isEmpty(specGroups)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specGroups);
    }
}
